<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">搜索：</label>
                        <div class="layui-input-inline mr0">
                            <input id="edtSearchAuth" class="layui-input" type="text" placeholder="输入关键字"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button id="btnSearchSysOrg" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索
                        </button>
                        <button id="btnAddSysOrg" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                        <button id="btnExpandSysOrg" class="layui-btn icon-btn">全部展开</button>
                    </div>
                    <div class="layui-inline">
                        <button id="btnFoldSysOrg" class="layui-btn icon-btn">全部折叠</button>
                    </div>
                </div>
            </div>

            <table class="layui-table" id="tableSysOrg" lay-filter="tableSysOrg"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="tableBarAuth">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="modelSysOrg">
    <form id="modelSysOrgForm" lay-filter="modelSysOrgForm" class="layui-form model-form">
        <input name="ID" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">上级</label>
            <div class="layui-input-block">
				<input id="parentId" type="text" name="PARENTID"   lay-filter="parentId" autocomplete="off" placeholder="请选择" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">名称</label>
            <div class="layui-input-block">
                <input name="NAME" placeholder="请输入名称" type="text" class="layui-input" maxlength="50"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">标识</label>
            <div class="layui-input-block">
                <input name="CODE" placeholder="请输入标识" type="text" class="layui-input"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <input name="STATE" type="radio" value="0" title="启用" checked/>
                <input name="STATE" type="radio" value="1" title="禁用"/>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">序号</label>
            <div class="layui-input-block">
                <input name="SORT" placeholder="请输入序号" type="number" class="layui-input" min="0" max="1000"
                       lay-verType="tips" lay-verify="required|number" required/>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="modelSubmitSysOrg" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
layui.extend({
    treeSelect: '/treeSelect/treeSelect'
}).use(['layer', 'form', 'table', 'admin', 'treetable', 'config','treeSelect'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var admin = layui.admin;
        var treetable = layui.treetable;
        var config = layui.config;
        var treeSelect=layui.treeSelect;

        // 渲染表格
        function renderTable() {
            treetable.render({
                treeColIndex: 1,
                treeSpid: 'root',
                treeIdName: 'ID',
                treePidName: 'PARENTID',
                elem: '#tableSysOrg',
                url:'#(ctx)/sys/org/listData',
                cellMinWidth: 100,
                cols: [[
                    {type: 'numbers'},
                    {field: 'NAME', title: '名称', minWidth: 200},
                    {field: 'CODE', title: '标识'},
                    {field: 'SORT', title: '序号', align: 'center'},
                    {
                        title: '状态', templet: function (d) {
                            var strs = ['<span class="layui-badge-rim" style="color:green;">启用</span>','<span class="layui-badge-rim" style="color:red;">禁用</span>'];
                            return strs[d.STATE];
                        }, align: 'center'
                    },
                    {templet: '#tableBarAuth', title: '操作', align: 'center', minWidth: 120}
                ]]
            });
        }

        renderTable();

        // 添加按钮点击事件
        $('#btnAddSysOrg').click(function () {
            showEditModel();
        });

        // 工具条点击事件
        table.on('tool(tableSysOrg)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            } else if (layEvent === 'del') { // 删除
                doDel(data);
            }
        });

        // 删除
        function doDel(data) {
            layer.confirm('确定要删除“【<span style="color:red;">' + data.NAME + '</span>】”吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (index) {
                layer.close(index);
                layer.load(2);
                admin.req('sys/org/delete', {
                    id: data.ID
                }, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.message, {icon: 1});
                        renderTable();
                    } else {
                        layer.msg(res.message, {icon: 2});
                    }
                }, 'get');
            });
        }

        // 显示表单弹窗
        function showEditModel(org) {
            admin.open({
                type: 1,
                title: (org ? '修改' : '添加') + '菜单',
                content: $('#modelSysOrg').html(),
                area:'400px',
                success: function (layero, dIndex) {
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                    var url = 'sys/org/save';
                    if (org && org.STATE == '2') {
                        $('#modelSysOrgForm input[name="STATE"][value="12"]').prop("checked", true);
                    }
                    else{
                        $('#modelSysOrgForm input[name="STATE"][value="0"]').prop("checked", true);
                    }
                    form.val('modelSysOrgForm', org);  // 回显数据
            	    treeSelect.render({
            	        elem: '#parentId',
            	        data: '#(ctx)/sys/org/selectTree',
            	        type: 'get',
            	        placeholder: '请选择',
            	        search: true,
            	        click: function(d){
            	        },
            	        success: function (d) {
            	        	if(org && org.PARENTID!='root')
                        	treeSelect.checkNode('parentId', org.PARENTID);
            	        }
            	    });
                    
                    // 表单提交事件
                    form.on('submit(modelSubmitSysOrg)', function (data) {
                        if (data.field.parentId == '') {
                            data.field.parentId = 'root';
                        }
                        layer.load(2);
                        var formData={}; 
                        for(var key in data.field){
                        	formData['sysOrg.'+key]=data.field[key];
                        }
                        admin.req(url,formData, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.close(dIndex);
                                layer.msg(res.message, {icon: 1});
                                renderTable();
                            } else {
                                layer.msg(res.message, {icon: 2});
                            }
                        }, 'get');
                        return false;
                    });
                }
            });
        }

        // 搜索按钮点击事件
        $('#btnSearchSysOrg').click(function () {
            $('#edtSearchAuth').removeClass('layui-form-danger');
            var keyword = $('#edtSearchAuth').val();
            var $tds = $('#tableSysOrg').next('.treeTable').find('.layui-table-body tbody tr td');
            $tds.css('background-color', 'transparent');
            if (!keyword) {
                layer.msg("请输入关键字", {icon: 5, anim: 6});
                $('#edtSearchAuth').addClass('layui-form-danger');
                $('#edtSearchAuth').focus();
                return;
            }
            var searchCount = 0;
            $tds.each(function () {
                if ($(this).text().indexOf(keyword) >= 0) {
                    $(this).css('background-color', '#FAE6A0');
                    if (searchCount == 0) {
                        $('body,html').stop(true);
                        $('body,html').animate({scrollTop: $(this).offset().top - 150}, 500);
                    }
                    searchCount++;
                }
            });
            if (searchCount == 0) {
                layer.msg("没有匹配结果", {icon: 5, anim: 6});
            } else {
                treetable.expandAll('#tableSysOrg');
            }
        });

        $('#btnExpandSysOrg').click(function () {
            treetable.expandAll('#tableSysOrg');
        });

        $('#btnFoldSysOrg').click(function () {
            treetable.foldAll('#tableSysOrg');
        });

    });
</script>